package com.igd.action.develop;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;

import com.igd.action.BaseAction;
import com.igd.common.constant.RequestConstant;
import com.igd.pojo.vo.ExtData;
import com.igd.pojo.vo.dev.PageCreateData;
import com.igd.pojo.vo.dev.PageFieldData;
import com.igd.utils.JsonUtil;
import com.igd.utils.dev.PageCreatUtil;
import com.igd.utils.ReflectionUtil;

 /**
 * @version  1.0
 *
 * @author   QIAOYU
 *
 * @date     2013-6-28
 *
 * @description	 开发工具包Action
 */

public class DevelopAction extends BaseAction {

	private static final long serialVersionUID = 9041382683464408730L;
	
	private Logger logger = Logger.getLogger(DevelopAction.class);
	
	/** 类名称 */
	private String className;
	
	
	private String pageInfo;
	
	/**
	 * 跳转到JsonEditor页面
	 * 
	 * @return
	 */
	public String toJsonEditor(){
		
		return SUCCESS;
	}
	
	/**
	 * 跳转到页面生成页
	 * 
	 * @return
	 */
	public String toGeneratePage(){
		
		return SUCCESS;
	}
	
	/**
	 * 获取指定类以及父类的声明字段
	 * 
	 * @return
	 */
	public String getClassField(){
		
		if(StringUtils.isNotEmpty(this.getClassName())){
			
			ExtData<String> extData = new ExtData<String>();

			try {
				
				Class<?> clz = Class.forName(this.getClassName());
				
				Field [] fields = ReflectionUtil.getFieldsWithSuper(clz);
				
				logger.debug("get fields size is " + fields.length);
				
				if(null != fields){
					
					ArrayList<String> fieldList = new ArrayList<String>(fields.length);
					
					for(Field f : fields){
						
						fieldList.add(f.getName());

					}
					
					extData.setRspData(fieldList);
				}
				
			} catch (ClassNotFoundException e) {
				
				extData.setStatusCode(ExtData.FAIL);
				
				extData.setMessage(e.getClass().getName());

			}
			
			this.put(RequestConstant.EXT_DATA_KEY,extData);

		}

		return this.JSON_PAGE;
	}
	
	/**
	 * 根据选定的字段创建页面
	 * 
	 * @return
	 * 
	 * @throws DocumentException
	 * 
	 * @throws IOException
	 */
	public String createPage() throws DocumentException, IOException{
		
		String pageInfo = this.getPageInfo();
		
		if(StringUtils.isNotEmpty(pageInfo)){
			
			PageCreateData page = JsonUtil.parseObject(pageInfo,PageCreateData.class);
			
			if(null != page){
				
				String fieldJsonStr = page.getFieldJson();
				
				List<PageFieldData> fieldList = JsonUtil.pasrseArray(fieldJsonStr, PageFieldData.class);
				
				if(null != fieldList && fieldList.size() > 0){
					
					page.setPageField(fieldList);
					
					PageCreatUtil util = PageCreatUtil.getInstance();
					
					String html = util.createPage(page);
					
					this.putExtData(html);
					
				}
			}
		}
		
		return this.JSON_PAGE;
	}
	
	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(String pageInfo) {
		this.pageInfo = pageInfo;
	}
}
